import Vue from 'vue'
import Router from 'vue-router'
import Index from '@/pages/Index/Index'

import HomePage from '@/pages/HomePage/Index'
import HotProduct from '@/pages/HotGoods/HotProduct'
import NewIndex from '@/pages/NewGoods/NewIndex'
import GoodsInfo from '@/public/components/GoodsInfo'
import About from '@/pages/About/AboutIndex'

Vue.use(Router);

const router = new Router({
  linkActiveClass: 'active',
  routes: [
    { path: '/', meta: {title: '首页'},redirect:'/index'},
    {
      path: '/index',
      name: 'Index',
      component: Index,
      meta: {
        title: '首页',
        keepAlive: true // 需要被缓存
      }
    },
    {
      path: '/home_page',
      name: 'HomePage',
      component: HomePage,
      meta: {
        title: '个人主页',
        keepAlive: true // 需要被缓存
      }
    },
    {
      path: '/hot_goods',
      name: 'HotProduct',
      component: HotProduct,
      meta: {
        title: '热门商品',
        keepAlive: true // 需要被缓存
      }
    },
    {
      path: '/new_goods',
      name: 'NewIndex',
      component: NewIndex,
      meta: {
        title: '新品上线',
        keepAlive: true // 需要被缓存
      }
    },
    {
      path: '/about',
      name: 'AboutIndex',
      component: About,
      meta: {
        title: '关于我们',
        keepAlive: true // 需要被缓存
      }
    },
    {
      path: '/goods_info',
      name: 'GoodsInfo',
      component: GoodsInfo,
      meta: {
        title: '商品详情',
        keepAlive: false // 不需要被缓存
      }
    },
  ],
  //  避免keep-alive造成进入不同新页面时
  //  初始位置不在顶部的bug
  scrollBehavior (to, from, savedPosition) {
    return { x: 0, y: 0 }
  }
});
//  动态渲染页面title
router.beforeEach((to, from, next) => {
  if(to.meta.title){
    document.title = to.meta.title
  }
  next()
});
export default router;
